package com.google.gac.masai.getit;

import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.ListPreference;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;


public class CustomListPreference extends ListPreference {
	Dialog mDialog;
	Context mContext;
	private final String tag = "CustomListPreference";
	private final String DROPBOX_CRED = "DROPBOX_CRED"; // drop box credentials 
	String test; //COMMENT THIS OUT LATER
	private SharedPreferences mPreferences;
	
	
	public CustomListPreference(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		
		
		/*
		CheckBoxPreference mCheckBoxPref = (CheckBoxPreference) findPreference("checkPref");
		PreferenceCategory mCategory = (PreferenceCategory) findPreference("category_foo");
		mCategory.removePreference(mCheckBoxPref);
*/
		// TODO Auto-generated constructor stub
		
	}

	



	public void onDialogClosed(boolean positiveResult) {
		test = getValue();
		super.onDialogClosed(positiveResult);
		if (positiveResult == false)
			setValue(test);
		
		if (this.getValue().equals("DropBox") && positiveResult == true) {
			
			mDialog = new Dialog(mContext);
			
			mDialog.setContentView(R.layout.popupwindow);
			mDialog.setTitle("Login to your DropBox account");
			
			EditText login = (EditText) mDialog.findViewById(R.id.login);
			EditText password = (EditText) mDialog.findViewById(R.id.password);
			
			Button loginButton = (Button) mDialog.findViewById(R.id.LoginButton);
			Button cancelButton = (Button) mDialog.findViewById(R.id.CancelLoginButton);
			
			
			
			mDialog.show(); 
			
			cancelButton.setOnClickListener(new OnClickListener() {
				
				//@Override
				public void onClick(View v) {
					setValue(test);
					mDialog.dismiss(); // cancel dialogs
				}
			}); 
			
			loginButton.setOnClickListener(new OnClickListener() {
				
				//@Override
				public void onClick(View v) {
					// Login to dropbox
					//showToast("Attempting Dropbox login");
					
					// Get username and password entered
					EditText login = (EditText) mDialog.findViewById(R.id.login);
					EditText password = (EditText) mDialog.findViewById(R.id.password);
					
					
					// To be done later : basic user-input checking here
					
					
					// Get username & passsword
					String user = login.getText().toString(); 
					String pass = password.getText().toString(); 
					Log.i(tag, "id: " +user+ " pass: " + pass);
					
					
					
					// Try to see if we have logged in before and already have access tokens
					
					
					//Jervis, can you see why it's not finding MODE_PRIVATE
					
					SharedPreferences data = mContext.getSharedPreferences(DropBox.CRED, Context.MODE_PRIVATE);
			        String dropbox_user = data.getString("DROPBOX_USER", null);
					String key = data.getString("ACCESS_KEY", null);
			        String secret = data.getString("ACCESS_SECRET", null);
			        
			        LoginCredAsync loginTask = new LoginCredAsync(mContext,user,pass);
			        loginTask.execute();
			        
			        mDialog.dismiss(); // close dialogs
				}
			
		});
		}
		else if (this.getValue().equals("File Name") && positiveResult == true) {
			mPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
			mDialog = new Dialog(mContext);
			
			mDialog.setContentView(R.layout.filenamespopup);
			mDialog.setTitle("Choose your file name categorization type");
			
			//EditText login = (EditText) mDialog.findViewById(R.id.login);
			//EditText password = (EditText) mDialog.findViewById(R.id.password);
			
			//Button loginButton = (Button) mDialog.findViewById(R.id.LoginButton);
			Button cancelButton = (Button) mDialog.findViewById(R.id.CancelLoginButton);
			
			cancelButton.setOnClickListener(new OnClickListener() {
				
				//@Override
				public void onClick(View v) {
					setValue(test);
					mDialog.dismiss(); // cancel dialogs
				}
			});
			
			CheckBox firstLetter = (CheckBox) mDialog.findViewById(R.id.Letter);
			if (mPreferences.getString("Filename Choice", "NOVALUE").equals("firstletter"))
				firstLetter.setChecked(true);
			firstLetter.setOnClickListener(new OnClickListener() {
				
				//@Override
				public void onClick(View v) {
					
					SharedPreferences.Editor edit = mPreferences.edit();
					edit.putString("Filename Choice", "firstletter");
					edit.commit();
					mDialog.dismiss(); // cancel dialogs
				}
			});
			
			CheckBox nameLength = (CheckBox) mDialog.findViewById(R.id.Length);
			if (mPreferences.getString("Filename Choice", "NOVALUE").equals("namelength"))
				nameLength.setChecked(true);
			nameLength.setOnClickListener(new OnClickListener() {
				
				//@Override
				public void onClick(View v) {
					SharedPreferences.Editor edit = mPreferences.edit();
					edit.putString("Filename Choice", "namelength");
					edit.commit();
					mDialog.dismiss(); // cancel dialogs
				}
			});
			
			
			mDialog.show(); 
			
			
		}

		
	}
	
}
